class Solution {
public:
    bool find(TreeNode* tree, TreeNode* x)
    {
        if(tree == nullptr)
            return false;
        
        return tree == x || find(tree->left,x) || find(tree->right,x);
    }
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
    {
        if(root == nullptr)
            return nullptr;
        if(root == p)
            return p;
        if(root == q)
            return q;
        
        bool pInL, pInR, qInL, qInR;
        pInL = find(root->left,p);
        pInR = !pInL;
        qInL = find(root->left,q);
        qInR = !qInL;


        if(qInL && pInL)
            return lowestCommonAncestor(root->left,p,q);
        else if(qInR && pInR)
            return lowestCommonAncestor(root->right,p,q);
        else
            return root;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    string tree2str(TreeNode* root) 
    {
        if(root == nullptr)
            return "";
        
        string str;
        str += to_string(root->val);

        //左右孩子其中一个不为空
        if(root->left || root->right)
        {
            str+='(';
            str += tree2str(root->left);
            str+=')';
        }
        
        //右孩子不为空
        if(root->right)
        {
            str+='(';
            str += tree2str(root->right);
            str+=')';
        }

        return str;
        
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    //将结点的路径入栈
    bool path(TreeNode* root, TreeNode* x,stack<TreeNode*>& st)
    {
        if(root == nullptr)
            return false;
        
        st.push(root);
        
        if(root == x)
            return true;

        if(path(root->left,x,st))
            return true;
        if(path(root->right,x,st))
            return true;        
            
        st.pop();
        return false;

        

    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
    {
        //定义栈存储结点的路径
        stack<TreeNode*> qPath,pPath;    
        path(root,q,qPath);
        path(root,p,pPath);

        //遍历路径当两个节点第一次相同就是最近公共祖先
        while(pPath.size() < qPath.size())
            qPath.pop();
        while(pPath.size() > qPath.size())
            pPath.pop();

        while(pPath.top() != qPath.top())
        {
            qPath.pop();
            pPath.pop();
        }

        return pPath.top();    
    }
};
/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
#include <sys/types.h>
class Solution {
public: 
	void InOrder(TreeNode* cur, TreeNode*& prev)
	{
		if(cur == nullptr)
			return;

		InOrder(cur->left, prev);
		cur->left = prev;

		if(prev)
			prev->right = cur;

		prev = cur;
		InOrder(cur->right, prev);
	}

    TreeNode* Convert(TreeNode* pRootOfTree) 
	{
		TreeNode* prev = nullptr;
		InOrder(pRootOfTree, prev);

		TreeNode* head = pRootOfTree;
		while(head && head->left)
		{
			head = head->left;
		}
		return head;
	}
};

